package com.itheima.googleplay9.ui.activity;

import android.animation.FloatEvaluator;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;

import com.itheima.googleplay9.R;
import com.itheima.googleplay9.adapter.MainPagerAdapter;
import com.itheima.googleplay9.bean.PageInfo;
import com.itheima.googleplay9.ui.fragment.CategoryFragment;
import com.itheima.googleplay9.ui.fragment.HomeFragment;
import com.itheima.googleplay9.ui.fragment.HotFragment;
import com.itheima.googleplay9.ui.fragment.RecommendFragment;
import com.itheima.googleplay9.ui.fragment.SubjectFragment;
import com.itheima.googleplay9.ui.view.PagerSlidingTab;
import com.itheima.googleplay9.util.LogUtil;
import com.itheima.googleplay9.util.Utils;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

public class MainActivity extends AppCompatActivity {
    @Bind(R.id.slidingTab)
    PagerSlidingTab slidingTab;
    @Bind(R.id.viewPager)
    ViewPager viewPager;
    @Bind(R.id.drawerLayout)
    DrawerLayout drawerLayout;
    @Bind(R.id.ll_main)
    LinearLayout ll_main;
    private ActionBarDrawerToggle drawerToggle;
    private ArrayList<PageInfo> pageInfos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //告诉Android系统对主线程访问网络和文君的检查策略不要这么严厉
        StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.LAX);

        //绑定当前Activity
        ButterKnife.bind(this);

        //1.初始化控件
//        drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
//        slidingTab = (PagerSlidingTab) findViewById(R.id.slidingTab);
//        viewPager = (ViewPager) findViewById(R.id.viewPager);

        //2.设置ActionBar
        setActionBar();

        //3.填充ViewPager
        pageInfos = preparePageInfo();
        MainPagerAdapter mainPagerAdapter = new MainPagerAdapter(getSupportFragmentManager(), pageInfos);
        viewPager.setAdapter(mainPagerAdapter);

        //4.绑定ViewPager和PagerSlidingTab
        slidingTab.setViewPager(viewPager);


        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {

            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * 准备页面信息
     * @return
     */
    private ArrayList<PageInfo> preparePageInfo() {
        ArrayList<PageInfo> infos = new ArrayList<>();
        String[] titles = Utils.getStringArray(R.array.tab_names);
        infos.add(new PageInfo(new HomeFragment(),titles[0]));
        infos.add(new PageInfo(new SubjectFragment(),titles[1]));
        infos.add(new PageInfo(new RecommendFragment(),titles[2]));
        infos.add(new PageInfo(new CategoryFragment(),titles[3]));
        infos.add(new PageInfo(new HotFragment(),titles[4]));
        return infos;
    }

    /**
     * 设置ActionBar
     */
    private void setActionBar() {
        ActionBar actionBar = getSupportActionBar();
//        actionBar.setTitle("呵呵");//设置标题
//        actionBar.setIcon(R.mipmap.ic_download);//设置图标

        actionBar.setDisplayShowHomeEnabled(true);//启用home按钮
        actionBar.setDisplayHomeAsUpEnabled(true);//显示出来home按钮

        //设置汉堡包按钮
        drawerToggle = new ActionBarDrawerToggle(this, drawerLayout,
                0, 0);
        drawerToggle.syncState();//同步DrawerLayout和ActionBar的状态

        //给汉堡包按钮添加旋转动画
        final FloatEvaluator evaluator = new FloatEvaluator();
        drawerLayout.addDrawerListener(drawerToggle);
        drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
//                LogUtil.e("ssssssss",""+slideOffset);
//                drawerView.setPivotX(0);
//                drawerView.setPivotY(0);
                //qq菜单的效果
                drawerView.setScaleX(evaluator.evaluate(slideOffset,0.4,1f));
                drawerView.setScaleY(evaluator.evaluate(slideOffset,0.4,1f));
//                //让主界面缩小
                ll_main.setScaleX(evaluator.evaluate(slideOffset,1f,0.8f));
                ll_main.setScaleY(evaluator.evaluate(slideOffset,1f,0.8f));
                ll_main.setTranslationX(evaluator.evaluate(slideOffset,0,ll_main.getWidth()/2));

                //翻转效果
//                drawerView.setRotationY(evaluator.evaluate(slideOffset,90,0f));
//                ll_main.setRotationY(evaluator.evaluate(slideOffset,0,-90f));
//                ll_main.setTranslationX(evaluator.evaluate(slideOffset,0,ll_main.getWidth()/2));

                //平移效果
//                drawerView.setTranslationY(evaluator.evaluate(slideOffset,-drawerView.getHeight(),0));
//                ll_main.setTranslationY(evaluator.evaluate(slideOffset,0,ll_main.getHeight()));
//                ll_main.setTranslationX(evaluator.evaluate(slideOffset,0,ll_main.getWidth()/2));
            }
            @Override
            public void onDrawerOpened(View drawerView) {
            }
            @Override
            public void onDrawerClosed(View drawerView) {
            }
            @Override
            public void onDrawerStateChanged(int newState) {
            }
        });
    }

    /**
     * 当点击ActionBar的home按钮的时候，会执行该方法
     *
     * @param item
     * @return
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
//        if(drawerLayout.isDrawerOpen(Gravity.LEFT)){
//            //如果是打开的，则关闭drawer
//            drawerLayout.closeDrawer(Gravity.LEFT);
//        }else {
//            //说明是关闭的，需要打开
//            drawerLayout.openDrawer(Gravity.LEFT);
//        }

        //更简单的做法
        drawerToggle.onOptionsItemSelected(item);

        return super.onOptionsItemSelected(item);
    }
}
